﻿/// <reference path="waa.d.ts" />
/// <reference path="filesystem.d.ts" />

class App {
    public static main(): void {
        var ctx = (<HTMLCanvasElement>document.getElementById('content')).getContext('2d');
        //var webgl = (<HTMLCanvasElement>document.getElementById('content')).getContext('experimental-webgl');
        var img = new Image();
        img.src = 'scream.jpg';
        var img2 = new Image();
        img2.src = 'svg/glossy_button-vertical.svg';

        setTimeout(function () {


            // text, rect
            ctx.font = "1.5em Microsoft Yahei";
            ctx.fillStyle = 'blue';
            ctx.fillRect(150, 150, 100, 100);
            ctx.fillText('Hello World! 你好中国', 0, 35);

            // circle
            ctx.beginPath();
            ctx.arc(400, 300, 110, 0, 2 * Math.PI, false);
            ctx.fillStyle = 'green';
            ctx.fill();

            // image
            ctx.drawImage(img, 10, 300);
            ctx.drawImage(img2, 400, 50, 600, 600*img2.height/img2.width);

            // copy
            var imgData = ctx.getImageData(220, 220, 120, 120);
            ctx.putImageData(imgData, 600, 400);
        }, 100);


        App.startAudio();
        //App.loadStorage();
        App.testLocalFS();
    }

    private static testLocalFS() {
        window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
        var webkitStorageInfo = (<any>window).webkitStorageInfo;


        webkitStorageInfo.requestQuota(window.PERSISTENT, 50 * 1024 * 1024, function (grantedBytes) {


            window.requestFileSystem(window.PERSISTENT, grantedBytes, function (fs) {
                console.info("succeeded requesting PERSISTENT storage " + grantedBytes);


                console.info(fs.name);
                var artifacts = 'fireemblum-artifacts-0.1';

                fs.root.getFile(artifacts, { create: false, exclusive: true }, function (fileEntry) {

                    console.info(fileEntry.isFile);
                    console.info(fileEntry.name);
                    console.info(fileEntry.fullPath);
                    console.info("artifact was loaded.");

                }, function () {
                        console.error("error finding artifact file");


                        fs.root.getFile(artifacts, { create: true, exclusive: true }, function (fileEntry) {

                            console.info(fileEntry.isFile);
                            console.info(fileEntry.name);
                            console.info(fileEntry.fullPath);
                            console.info("artifact was created.");

                        }, function () {
                                console.error("error creating artifact file");
                            });
                    });


            }, function () {
                    console.info("error requesting PERSISTENT storage");
                });


        }, function (e) {
                console.log('Error', e);
            });


    }

    private static loadStorage(): void {
        console.log(`localStorage.remainingSpace=${localStorage.remainingSpace}`);

        var data = localStorage.getItem('app');
        if (!data) {
            localStorage.setItem('app', 'some data');
            data = localStorage.getItem('app');
            console.log('data stored');
        } else {
            console.log('get data from local storage');
        }

        console.log(`app=${data}`);

        //var bb = new BlobBuilder();
       
    }

    private static startAudio(): void {
        var context = new AudioContext();
        //var dogBarkingBuffer;

        function loadDogSound(url) {
            //var buff = localStorage.getItem(url);
            //if (buff) {
            //    context.decodeAudioData(buff, function (buffer) {
            //        //dogBarkingBuffer = buffer;

            //        playSound(buffer);
            //    }, function () {
            //            console.log("error decoding mp3");
            //        });

            //    return;
            //}


            var request = new XMLHttpRequest();
            request.open('GET', url, true);
            request.responseType = 'arraybuffer';

            // Decode asynchronously
            request.onload = function () {

                //localStorage.setItem(url, request.response);

                context.decodeAudioData(request.response, function (buffer) {
                    //dogBarkingBuffer = buffer;

                    playSound(buffer);
                }, function () {
                        console.log("error decoding mp3");
                    });
            };

            request.onerror = function () {
                console.log("error loading mp3");
            };

            request.send();
        }

        function playSound(buffer) {
            var source = context.createBufferSource(); // creates a sound source
            source.buffer = buffer;                    // tell the source which sound to play
            source.connect(context.destination);       // connect the source to the context's destination (the speakers)
            source.start(0);                           // play the source now
            // note: on older systems, may have to use deprecated noteOn(time);
        }

        loadDogSound("sound/Neptune Illusion Dennis Kuo.mp3");
    }



}
App.main();